//
//  LCTimeLineViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-18.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCTimeLineViewController.h"

@interface LCTimeLineViewController ()
@property (nonatomic) LCTimeLineModel *timeLineModel;
@property (nonatomic) CGSize imageSize;
@end

@implementation LCTimeLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDrawButton];
    [self creatCollection];
    [self registerNib];
    [self fetchWebDataWith:KTimeLineHeader body:[NSString stringWithFormat:kTimeLineBody,self.page]];
    [self creatRefresh];
}

- (void)registerNib
{
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCTimeLineCell" bundle:nil] forCellWithReuseIdentifier:@"LCTimeLineCell"];
    
}
- (void)creatRefresh
{
    __weak typeof(self) weakSelf = self;
    [self.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 0;
        [weakSelf fetchWebDataWith:KTimeLineHeader body:[NSString stringWithFormat:kTimeLineBody,weakSelf.page]];
    }];
    [self.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page+=10;
        if (weakSelf.page>=weakSelf.timeLineModel.total.integerValue) {
            [MMProgressHUD show];
            [MMProgressHUD  dismissWithSuccess:@"到底了..."];
            [weakSelf.collectionView footerEndRefreshing];
            return ;
        }
        [weakSelf fetchWebDataWith:KTimeLineHeader body:[NSString stringWithFormat:kTimeLineBody,weakSelf.page]];
    }];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    NSDictionary *dict = dataDict[@"data"];
    if (self.page==0) {
        self.timeLineModel = [[LCTimeLineModel alloc]initWithDictionary:dict error:nil];
    }
    else
    {
        LCTimeLineModel *model = [[LCTimeLineModel alloc]initWithDictionary:dict error:nil];
        [self.timeLineModel.list addObjectsFromArray:model.list]; 
    }
    
    [self.collectionView reloadData];
    
}
#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.timeLineModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTimeLineCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCTimeLineCell" forIndexPath:indexPath];
    LCTLListModel *model = self.timeLineModel.list[indexPath.row];
    [cell showModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     LCTLListModel *model = self.timeLineModel.list[indexPath.row];
    if ([model.coverimg isEqualToString:@""]) {
        return CGSizeMake(kScreenSize.width-20, 120);
    }
    else
    {
        NSString *wh = model.coverimg_wh;
        NSRange range = [wh rangeOfString:@"*"];
        NSString *width = [wh substringToIndex:range.location];
        NSString *hight = [wh substringFromIndex:range.location+1];
        CGFloat w = width.floatValue;
        CGFloat h = hight.floatValue;
        CGSize size = CGSizeMake(kScreenSize.width-20, h*(kScreenSize.width-20)/w+120);
     return size;
        
    }
    return CGSizeMake(0, 0);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
    LCTLListModel *model = self.timeLineModel.list[indexPath.row];
    LCTimeLineDetailViewController *detail = [[LCTimeLineDetailViewController alloc]init];
    detail.contentID = model.contentid;
    [self.navigationController pushViewController:detail animated:YES];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
    }
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(0, 0);
}


@end
